package com.teamwork.module01.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teamwork.common.entity.module01.Class;
import com.teamwork.common.entity.module01.Subject;
import com.teamwork.common.result.Result;
import com.teamwork.module01.mapper.SubjectMapper;
import com.teamwork.module01.service.SubjectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {
    /**
     * 根据名称获取专业信息
     * @param name
     * @return
     */
    @Override
    public Result getByName(String name) {
        LambdaQueryWrapper<Subject> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Subject::getSubjectName,name).or().like(Subject::getSubjectName,name);
        Subject one = this.getOne(wrapper);
        if(one==null)
            return Result.failure("该专业不存在");

        return Result.success(one);
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Result getPage(Integer page, Integer size) {
        Page<Subject> subjectPage = new Page<>(page,size);
        this.page(subjectPage);
        return Result.success(subjectPage);
    }

    /**
     * 添加或者修改
     * @param subject
     * @return
     */
    @Override
    public Result InsertOrUpdate(Subject subject) {
        if(subject.getId()!=null){
            //修改
            boolean b = this.updateById(subject);
            if(!b) {
                log.error("专业信息为:"+subject+" 修改失败");
                return Result.failure("修改失败");
            }
            log.info("修改了一条专业信息:{}",subject);
            return Result.success("修改成功");
        }
        //添加
        //先判断有没有该专业
          LambdaQueryWrapper<Subject> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Subject::getSubjectName,subject.getSubjectName());
        Subject one = this.getOne(wrapper);
        if(one!=null){
            //专业已经存在
            return Result.failure("该专业已经存在，请重新添加");
        }
        //专业不存在，可以添加
        boolean sb = this.save(subject);
        if(!sb) {
            log.error("专业信息为:"+subject+"添加失败");
            return Result.failure("添加失败");
        }
         log.info("添加一条专业信息为:{}",subject);
        return  Result.success("添加成功");
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @Override
    public Result deleteById(Integer id) {
        return null;
    }

    /**
     * id数组批量删除
     * @param idList
     * @return
     */
    @Override
    public Result batchDelete(List<Integer> idList) {
        for (Integer id : idList) {
            boolean b = this.removeById(id);
            if(!b) {
                log.error("删除专业id为:"+id+" 的记录失败!");
                return Result.failure("删除失败");
            }
            log.info("删除了一条专业信息，id为:{}",id);
        }

        return Result.success("删除成功");
    }
}
